package com.idconfirmsystem.activity;

import java.util.ArrayList;
import java.util.List;

import org.kymjs.kjframe.KJActivity;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.idconfirmsystem.R;
import com.idconfirmsystem.fragment.RecevieMsgFragment;
import com.idconfirmsystem.fragment.SendMsgFragment;

@SuppressLint("ResourceAsColor")
public class NoticeActivity extends KJActivity {
	private ViewPager mPageVp;

	private List<Fragment> mFragmentList = new ArrayList<Fragment>();
	private FragmentAdapter mFragmentAdapter;

	/**
	 * Tab显示内容TextView
	 */
	private TextView mRecTv;
	private TextView mSendTv;
	/**
	 * Tab的引导线
	 */
	private ImageView mTabLineIv;
	/**
	 * Fragment
	 */
	private RecevieMsgFragment mrecMsgFragment;
	private SendMsgFragment mSendMsgFragment;
	/**
	 * ViewPager的当前选中页
	 */
	private int currentIndex;
	/**
	 * 屏幕的宽度
	 */
	private int screenWidth;

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_notice);

		ActionBar ab = getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);
		ab.setHomeButtonEnabled(true);

		mPageVp = (ViewPager) findViewById(R.id.id_page_vp);
		mRecTv = (TextView) findViewById(R.id.tv_msg_rec);
		mSendTv = (TextView) findViewById(R.id.tv_msg_send);
		mTabLineIv = (ImageView) findViewById(R.id.id_tab_line_iv);
		init();
		initTabLineWidth();
	}

	private void setFocus(int position) {
		mPageVp.setCurrentItem(position);

	}

	private void init() {
		mSendMsgFragment = new SendMsgFragment();
		mrecMsgFragment = new RecevieMsgFragment();
		mFragmentList.add(mrecMsgFragment);
		mFragmentList.add(mSendMsgFragment);

		mFragmentAdapter = new FragmentAdapter(this.getSupportFragmentManager(), mFragmentList);
		mPageVp.setAdapter(mFragmentAdapter);
		mPageVp.setCurrentItem(0);

		mRecTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setFocus(0);
			}
		});
		mSendTv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setFocus(1);
			}
		});
		mPageVp.setOnPageChangeListener(new OnPageChangeListener() {

			/**
			 * state滑动中的状态 有三种状态（0，1，2） 1：正在滑动 2：滑动完毕 0：什么都没做。
			 */
			@Override
			public void onPageScrollStateChanged(int state) {
			}

			/**
			 * position :当前页面，及你点击滑动的页面 offset:当前页面偏移的百分比
			 * offsetPixels:当前页面偏移的像素位置
			 */
			@Override
			public void onPageScrolled(int position, float offset, int offsetPixels) {
				LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv.getLayoutParams();
				Log.e("offset:", offset + "");
				if (currentIndex == 0 && position == 0) {
					lp.leftMargin = (int) (offset * (screenWidth * 1.0 / 2) + currentIndex * (screenWidth / 2));
				} else if (currentIndex == 1 && position == 0) {
					lp.leftMargin = (int) (-(1 - offset) * (screenWidth * 1.0 / 2) + currentIndex * (screenWidth / 2));
				}
				mTabLineIv.setLayoutParams(lp);
			}

			@Override
			public void onPageSelected(int position) {
				resetTextView();
				switch (position) {
				case 0:
					mRecTv.setTextColor(Color.rgb(64, 169, 82));
					break;
				case 1:
					mSendTv.setTextColor(Color.rgb(64, 169, 82));
					break;
				}
				currentIndex = position;
			}
		});

	}

	/**
	 * 设置滑动条的宽度为屏幕的1/2(根据Tab的个数而定)
	 */
	private void initTabLineWidth() {
		DisplayMetrics dpMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(dpMetrics);
		screenWidth = dpMetrics.widthPixels;
		LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLineIv.getLayoutParams();
		lp.width = screenWidth / 2;
		mTabLineIv.setLayoutParams(lp);
	}

	/**
	 * 重置颜色
	 */
	private void resetTextView() {
		mRecTv.setTextColor(Color.GRAY);
		mSendTv.setTextColor(Color.GRAY);
	}

	public class FragmentAdapter extends FragmentPagerAdapter {
		List<Fragment> fragmentList = new ArrayList<Fragment>();

		public FragmentAdapter(FragmentManager fm, List<Fragment> fragmentList) {
			super(fm);
			this.fragmentList = fragmentList;
		}

		@Override
		public Fragment getItem(int position) {
			return fragmentList.get(position);
		}

		@Override
		public int getCount() {
			return fragmentList.size();
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
